<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
	<th:block th:include="include :: header('新增PK对战')" />
</head>
<body class="gray-bg">
	
	<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>新增PK对战</h5>
					</div>
					<div class="ibox-content">
						<form class="form-horizontal m" id="form-pk-add" th:object="${pkBattle}">
							<div class="form-group">
								<label class="col-sm-3 control-label">对战编号：</label>
								<div class="col-sm-8">
									<input name="battleNo" class="form-control" type="text" readonly>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">发起者：</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input type="hidden" name="initiatorId" id="initiatorId" th:value="${currentUserPartyMember != null ? currentUserPartyMember.id : ''}">
										<input class="form-control" type="text" id="initiatorName" readonly required th:value="${currentUserPartyMember != null ? currentUserPartyMember.name : ''}">
                                        <span class="input-group-btn">
                                            <button type="button" class="btn btn-primary" onclick="selectInitiator()" th:disabled="${currentUserPartyMember != null}"><i class="fa fa-search"></i> 选择</button>
                                        </span>
									</div>
                                    <span class="help-block m-b-none" th:if="${currentUserPartyMember == null}"><i class="fa fa-info-circle"></i> 当前用户未关联党员信息</span>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">应战者：</label>
								<div class="col-sm-8">
									<div class="input-group">
										<input type="hidden" name="challengerId" id="challengerId">
										<input class="form-control" type="text" id="challengerName" readonly required>
										<span class="input-group-btn">
											<button type="button" class="btn btn-primary" onclick="selectChallenger()"><i class="fa fa-search"></i> 选择</button>
										</span>
									</div>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">场次配置：</label>
								<div class="col-sm-8">
									<select name="configId" id="configId" class="form-control" required>
										<option value="">请选择场次配置</option>
									</select>
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">对战状态：</label>
								<div class="col-sm-8">
									<select name="battleStatus" class="form-control" required>
										<option value="1">等待应战</option>
										<option value="2">对战中</option>
										<option value="3">已结束</option>
									</select>
								</div>
							</div>
<!--							<div class="form-group">-->
<!--								<label class="col-sm-3 control-label">获胜者ID：</label>-->
<!--								<div class="col-sm-8">-->
<!--									<input name="winnerId" class="form-control" type="number">-->
<!--								</div>-->
<!--							</div>-->
							<div class="form-group">
								<label class="col-sm-3 control-label">开始时间：</label>
								<div class="col-sm-8">
									<input name="startTime" class="form-control" type="datetime-local">
								</div>
							</div>
							<div class="form-group">
								<label class="col-sm-3 control-label">结束时间：</label>
								<div class="col-sm-8">
									<input name="endTime" class="form-control" type="datetime-local">
								</div>
							</div>
							<div class="form-group">
								<div class="form-control-static col-sm-offset-9">
									<button type="submit" class="btn btn-primary">提交</button>
									<button type="button" class="btn btn-danger" onclick="closeItem()">关闭</button>
								</div>
							</div>
						</form>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<th:block th:include="include :: footer" />
	
	<script type="text/javascript">
		var prefix = ctx + "system/exam/pk";
		
		// 自动生成对战编号
		$(function() {
			var timestamp = new Date().getTime();
			$("input[name='battleNo']").val("mlpk" + timestamp);
			
			// 加载场次配置下拉列表（只加载PK答题类型且启用的配置）
			loadConfigOptions();
		});
		
		// 加载场次配置下拉选项
		function loadConfigOptions() {
			$.ajax({
				url: ctx + "system/exam/config/list",
				type: "post",
				data: {
					examType: 2, // PK答题类型
					status: 1    // 启用状态
				},
				success: function(result) {
					if (result.code == 0) {
						var options = '<option value="">请选择场次配置</option>';
						for (var i = 0; i < result.rows.length; i++) {
							var config = result.rows[i];
							options += '<option value="' + config.id + '">' + config.configName + '</option>';
						}
						$("#configId").html(options);
					} else {
						$.modal.alertError("加载场次配置失败: " + result.msg);
					}
				},
				error: function() {
					$.modal.alertError("加载场次配置失败");
				}
			});
		}
		
		$("#form-pk-add").validate({
			rules: {
				initiatorId: {
					required: true,
					digits: true
				},
				challengerId: {
					required: true,
					digits: true
				},
				configId: {
					required: true,
					digits: true
				}
			},
			messages: {
				initiatorId: {
					required: "请选择发起者",
					digits: "发起者必须为整数"
				},
				challengerId: {
					required: "请选择应战者",
					digits: "应战者必须为整数"
				},
				configId: {
					required: "请选择场次配置",
					digits: "场次配置必须为整数"
				}
			},
			submitHandler: function(form) {
				// 校验发起者和应战者不能是同一个人
				var initiatorId = $("#initiatorId").val();
				var challengerId = $("#challengerId").val();
				if (initiatorId && challengerId && initiatorId == challengerId) {
					$.modal.alertError("发起者和应战者不能是同一个人");
					return false;
				}
				$.operate.save(prefix + "/add", $('#form-pk-add').serialize());
			}
		});
		
		// 选择发起者（党员）
		function selectInitiator() {
			// 使用layer.open方式打开党员选择窗口
			layer.open({
				type: 2,
				title: "选择发起者",
				area: ['800px', '500px'],
				content: ctx + "system/partymember/member/select",
				btn: ['确定', '关闭'],
				yes: function(index, layero) {
					var iframeWin = layero.find('iframe')[0];
					var data = iframeWin.contentWindow.getSelected();
					if (data && data.length > 0) {
						$("#initiatorId").val(data[0].id);
						$("#initiatorName").val(data[0].name);
					}
					layer.close(index);
				}
			});
		}
		
		// 选择应战者（党员）
		function selectChallenger() {
			// 使用layer.open方式打开党员选择窗口
			layer.open({
				type: 2,
				title: "选择应战者",
				area: ['800px', '500px'],
				content: ctx + "system/partymember/member/select",
				btn: ['确定', '关闭'],
				yes: function(index, layero) {
					var iframeWin = layero.find('iframe')[0];
					var data = iframeWin.contentWindow.getSelected();
					if (data && data.length > 0) {
						$("#challengerId").val(data[0].id);
						$("#challengerName").val(data[0].name);
					}
					layer.close(index);
				}
			});
		}
		
		function closeItem() {
			var index = parent.layer.getFrameIndex(window.name);
			parent.layer.close(index);
		}
	</script>
</body>
</html>